<?php

namespace App\Http\Controllers\Front;

use App\Interfaces\GoodsService;
use App\Services\Allinpay\Services\Response;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Interfaces\CartService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use InvalidArgumentException;
use App\Interfaces\OrderService;
use Session;

class CartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(CartService $cartService)
    {

        $cart = $cartService->getInfo();
        //dd($cart);



        return view('front.cart.index',['cart'=>$cart]);

    }//end func index()


    /**
     * 结算购物车
     *
     * @return \Illuminate\Http\Response
     */
    public function pay(OrderService $orderService, Request $request)
    {
        $cartGoods = $request->get('cartGoods');

        Session::put('cartGoods',$cartGoods);

        Session::save();


        if (is_array(Session::get('cartGoods'))){

            return response()->json(['status'=>'success']);


        }else{

            return response()->json(['status'=>'failed','message'=>'参数错误']);

        }



    }//end func create


    /**
     * 快速结算
     *
     * @return \Illuminate\Http\Response
     */
    public function fastPay(OrderService $orderService, Request $request)
    {
        $cartGoods = $request->get('cartGoods');

        Session::put('fastPay',$cartGoods);

        Session::save();

        if (is_array(Session::get('fastPay'))){

            return response()->json(['status'=>'success']);


        }else{

            return response()->json(['status'=>'failed','message'=>'参数错误']);

        }


    }//end func create


    /**
     * 添加商品到购物车
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, CartService $cartService)
    {
        $return = ['message'=>''];
        $goods_id = $request->goods_id;
        $goods_num = $request->goods_num;

        if(empty($goods_id)){
            $return['message'] = '商品id不能为空';
        }//end if()

        if(empty($goods_num)){
            $return['message'] = '商品数量不能为空';
            $return['message'] = '商品数量不能为空';
        }//end if()

        try{

            $cartService->addGoods($goods_id,$goods_num);
            //$cartInfo = $cartService->getInfo();
            //$return['goods_num'] = $cartInfo['total_number'];

            $cartNum=$cartService->getCartNumber();
            $return['goods_num'] = $cartNum;

        } catch (InvalidArgumentException $e) {

            $return['message'] = $e->getMessage();

        }//end try

        return Response()->json($return);
    }



    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id, CartService $cartService, Request $request)
    {

        $return = ['message'=>''];
        $goods_ids = $request->goods_ids;
        if(empty($goods_ids)){

            $return['message'] = '要删除的商品id数组不能为空';

        }else{

            try {

                $cartService->removeGoods($goods_ids);

            } catch (InvalidArgumentException $e) {

                $return['message'] = $e->getMessage();

            }//end try

        }//end if()

        return Response()->json($return);
    }


    /**
     * 获取商品数组中商品总数、总价、总关税等信息
     *
     * @param array $sel_id : 当前更改的商品id
     *
     * @return mixed 1. 总价,总个数,总关税,更改的商品关税  {"total_number":3,"total_price":"2204.77","total_tariff_price":"222.81","tariff_price":199.9}
     */
    public function getGoodsInfo($goods_id, CartService $cartService, Request $request,GoodsService $goodsService){



        $cartGoods = $request->get('cartGoods');


        $buy_num = 0;

        foreach($cartGoods as $v){
            if ($goods_id==$v['goods_id']){
                $buy_num = $v['buy_num'];
            }
        }


        $cartService->updateNumber($goods_id,$buy_num);


        $cart = $cartService->getGoodsInfo($cartGoods);



        return response()->json(['info'=>$cart]);









    }//end func getGoodsInfo

}
